import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getOutcomes } from '@/api/outcomes'
import { ElMessage } from 'element-plus'

export const useOutcomesStore = defineStore('outcomes', () => {
  // 状态
  const tableData = ref([])
  const total = ref(0)
  const pageNum = ref(1)
  const pageSize = ref(10)
  const dateRange = ref([null, null])
  const sortBy = ref('desc')
  const loading = ref(false)
  const searchText = ref('')

  // 获取活动成果列表
  const fetchOutcomesData = async () => {
    loading.value = true
    try {
      const response = await getOutcomes({
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        sortType: sortBy.value,
        startDate: dateRange.value[0] ? dateRange.value[0].toISOString().split('T')[0] : null,
        endDate: dateRange.value[1] ? dateRange.value[1].toISOString().split('T')[0] : null,
        searchText: searchText.value
      })
      
      if (response.code === 200) {
        tableData.value = response.data.items
        total.value = response.data.total
      } else {
        ElMessage.error(response.msg || '获取活动成果列表失败')
      }
    } catch (error) {
      console.error('获取活动成果列表错误:', error)
      ElMessage.error('获取活动成果列表失败')
    } finally {
      loading.value = false
    }
  }

  return {
    tableData,
    total,
    pageNum,
    pageSize,
    dateRange,
    sortBy,
    loading,
    searchText,
    fetchOutcomesData
  }
})